<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    @close="dialogCLose()"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item
        label="基本单位"
        prop="basicUnit"
        :class="{ 'is-required': !dataForm.id }"
      >
        <el-input
          v-model="dataForm.basicUnit"
          placeholder="基本单位"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="副单位"
        prop="otherUnit"
      >
        <el-col>
          <el-input
            v-model="dataForm.otherUnit"
            placeholder="副单位"
            style="width: 200px"
          ></el-input
          >=
          <el-input
            v-model="dataForm.ratio"
            placeholder="主单位"
            style="width: 200px"
          ></el-input
          >{{ dataForm.basicUnit }}
        </el-col>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :disabled="btnstatus"
        >确定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
import { isEmail, isMobile } from "@/utils/validate";
export default {
  data() {
    return {
      btnstatus: false,
      parentValue: [],
      value1: "",
      value2: "",
      value: "",
      visible: false,
      roleList: [],
      dataForm: {
        id: 0,
        basicUnit: "",
        otherUnit: "",
        ratio: "",
      },
      dataRule: {
        basicUnit: [
          { required: true, message: "基本单位不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    init(id, basicUnit, otherUnit, ratio) {
      // console.log(id,basicUnit,otherUnit,ratio)
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.dataForm.id = id;
          this.dataForm.basicUnit = basicUnit;
          this.dataForm.otherUnit = otherUnit;
          this.dataForm.ratio = ratio;
        }
      });
    },

    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          let info = {
            name:
              this.dataForm.basicUnit +
              "," +
              this.dataForm.otherUnit +
              "(1:" +
              this.dataForm.ratio +
              ")",
            basicUnit: this.dataForm.basicUnit,
            otherUnit: this.dataForm.otherUnit,
            ratio: this.dataForm.ratio,
          };
          let url = this.$http.adornUrl("/unit/add");
          if (this.dataForm.id) {
            url = this.$http.adornUrl(`/unit/update?id=${this.dataForm.id}`);
          }
          this.$http({
            url,
            method: "post",
            data: this.$http.adornData(info, false),
          }).then(({ data }) => {
            console.log(data);
            if (data && data.code === 0) {
              this.btnstatus = true;
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },

    dialogCLose() {
            this.btnstatus = false;
    },
  },
};
</script>
